package KVRMIService;

import java.rmi.*;
import java.util.HashMap;
import java.util.Map;

/**
 * Remote Class for the KVStore
 */
public class KVStore extends java.rmi.server.UnicastRemoteObject implements KVStoreInterface {
	
	// Member variables
	String processID = null;
	// Map that contains the (key,value) pairs
	private Map<Integer, String> kvStore;
	
	/**
	 * Public constructor
	 * 
	 * @throws RemoteException
	 */
	public KVStore() throws RemoteException {
		super();
		
		// Initialize the key value store for this handler
	    kvStore = new HashMap<Integer, String>();
	}
	
	/**
	 * Function to lookup the value for the given key
	 * 
	 * @throws RemoteException
	 */
	public String lookup(int key) throws RemoteException {
		// Return the value associated with the key
		return kvStore.get(key);
	}
	
	/**
	 * Insert the (key,value) pair in this node. Key must be in this node's key range.
	 * 
	 * @throws RemoteException
	 */
	public void insert(int key, String value) throws RemoteException {
	    // Store the key,value pair
	    kvStore.put(key, value);
	}

	/**
	 * Delete the (key,value) pair from this node. Key must be stored at this node.
	 * 
	 * @throws RemoteException
	 */
	public void deleteKey(int key) throws RemoteException {
	    // Remove the key,value pair
	    kvStore.remove(key);
	}
}
